url = "http://osja.googlecode.com/svn/trunk/";
	
function log(text)
{
	document.getElementById("log").value += text + "\r\n";
	return 1;
}

function isVowel(ch)
{
	return "aeiouAEIOU".indexOf(ch) != -1;
}

function isConsonant(ch)
{
	return "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ".indexOf(ch) != -1;
}

function stripAccents(str)
{
	var accents =
	[
		/[\xC0-\xC5]/g, /[\xE0-\xE5]/g,
		/[\xC8-\xCB]/g, /[\xE8-\xEB]/g,
		/[\xCC-\xCF]/g, /[\xEC-\xEF]/g,
		/[\xD2-\xD6]/g, /[\xF2-\xF6]/g,
		/[\xD9-\xDC]/g, /[\xF9-\xFC]/g,
		/[\xC7]/g, /[\xE7]/g
	];
	for(var i = 0; i < accents.length; ++i)
		str = str.replace(accents[i], ['A','a','E','e','I','i','O','o','U','u','C','c'][i]);
	return str;
}

function toRomaji(inp)
{
	var otp = "";
	var cnt = 0;
	var k = "";
	var l = "";
	inp = stripAccents(inp.toLowerCase().replace(/\x20+/g, " "));

	while(cnt < inp.length)
	{
		k = inp.charAt(cnt);
		l = cnt + 1 > inp.length - 1 ? "u" : inp.charAt(cnt + 1);

		if(isConsonant(k) && k == l && k == inp.charAt(cnt + 2))
		{
			++cnt;
			continue;
		}
		if(isConsonant(k) && k == l && cnt != 0 && inp.charAt(cnt - 1) != k)
		{
			otp += "+tsu";
			++cnt;
			continue;	
		}
		if(otp.length)
		{
			otp += "-";
		}
		if(k == " " || k.charCodeAt(0) == 0x3000)
		{
			otp += "_";
			++cnt;
			continue;
		}
		if(l == "y" && !isVowel(k))
		{
			l = "i";
		}
		if(k == "x")
		{
			k = (cnt == 0) ? "z" : "k";
			otp += k;
			++cnt;
			if(!isVowel(l))
			{
				otp += "u";
			}
			else
			{
				otp += l;
				++cnt;	
			}
		}
		else if(k == "j")
		{
			if(l == "a" || l == "u" || l == "o")
			{
				k = "j";
				otp += k + "i+y" + l;
				cnt += 2;
			}
			else
			{
				otp += "ji";
				cnt += 2;
			}
		}
		else if(k == "y")
		{
			if(l == "a" || l == "u" || l == "o")
			{
				otp += k + l;
				cnt += 2;
			}
			else 
			{
				k = "i";
				otp += k;
				++cnt;
			}
		}
		else if(k == "m")
		{
			if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else if(l == "b" || l == "m" || l == "p")
			{
				otp += "n";
				++cnt;
			}    
			else 
			{
				otp += "mu";
				++cnt;
			}
		}
		else if(isVowel(k) || (k == "n" && !isVowel(l)))
		{
			otp += k;
			++cnt;
		}
		else if(k == "w")
		{
			if(l == "a" || l == "o")
			{
				otp += k + l;
				cnt += 2;
			}
			else
			{
				k = "b";
				otp += k;
				++cnt;
				if(isVowel(l))
				{
					otp += l;
					++cnt;	
				}
				else
				{
					otp += "u";
				}
			}
		}
		else if(k == "b" || k == "g" || k == "k" || k == "r")
		{
			if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else
			{
				otp += k + "u";
				++cnt;
			}
		}
		else if(k == "d")
		{
			if(isVowel(l))
			{
				otp += k + l; //di = ji, du = ju (pronunciation)
				cnt += 2;
			}
			else
			{
				otp += "de";
				++cnt;
			}
		}
		else if(k == "f")
		{
			otp += "fu";
			cnt += isVowel(l) ? 2 : 1;
		}
		else if(k == "h")                  
		{
			if(l == "u")
			{
				otp += "fu";
				cnt += 2;
			}
			else if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else
			{
				otp += "fu";
				++cnt;
			}
		}
		else if(k == "z")
		{
			if(l == "i")
			{
				k = "j";
				otp += k + l;
				cnt += 2;
			} 
			else if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else
			{
				otp += "zu";
				++cnt;
			}
		}
		else if(k == "n")
		{
			if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
		}
		else if(k == "p")
		{
			if(l == "h")
			{
				otp += "fu"; 
				cnt += 2;
			}
			else if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else
			{
				otp += "pu";
				++cnt;
			}
		}
		else if(k == "s" || k == "c")
		{
			var tmp = inp.charAt(cnt + 2);
			if(k == "c")
			{
				k = "k";
			}
			if((l == "i" && k != "k") || (l == "h" && tmp == "i"))
			{
				if(k == "k")
				{
					k = "c";
				}
				otp += k + "hi";
				cnt += l == "h" ? 3 : 2;
			}
			else if(isVowel(l))
			{
				otp += k + l;
				cnt += 2;
			}
			else if(l == "h" && isVowel(tmp) && tmp != "i")
			{
				if(k == "k")
				{
					k = "c";
				}
				otp += k + "hi" + ((tmp == "e") ? ("") : ("+y" + tmp));
				cnt += 3;
			}
			else
			{
				otp += k + "u";
				++cnt;
			}
		}
		else if(k == "q")
		{
			k = "ku";
			otp += k;
			cnt += (l == "u") ? 2 : 1;
			if(l != "u" && isVowel(inp.charAt(cnt)))
			{
				otp += "+" + inp.charAt(cnt);
				++cnt;
			}
		}
		else if(k == "t")
		{
			if(l == "a" || l == "e" || l == "o")
			{
				otp += k + l;
				cnt += 2;
			}
			else if(l == "u" || (!isVowel(l) && l != "h"))
			{
				otp += "tsu";
				++cnt;
	
				if(l == "u")
				{
					++cnt;
				} 
				if(l == "s" && inp.charAt(cnt + 1) == "u")
				{
					cnt += 2;
				}
			}
			else if(l == "i")
			{
				otp = otp + "chi";
				cnt += 2;
			}
			else if(l == "h")
			{
				otp += "fu";
				cnt += 2;
			}
		}
		else
		{
			switch(k)
			{
				case "k":
				{
					k = "k" + ((isVowel(l)) ? (l) : ("u"));
					++cnt;		
					break;
				}
				case "v":
				{
					k = "b" + ((isVowel(l)) ? (l) : ("u"));
					++cnt;
					break;
				}
				case "l":
				{
					k = "r" + ((isVowel(l)) ? (l) : ("u"));
					++cnt;
					break;
				}
				default:
				{
					k = "";
					break;
				}
			}
			otp += k;
			++cnt;
		}
	}
	return otp;
}

function doIt(inp)
{
	if(!inp.length)
	{
		inp = "no name"; //temp
		//return;
	}
	document.getElementById('display').innerHTML = "";
	document.getElementById('display').innerHTML += inp + "<br />";
	var syl = inp.split("-");
	for(var i in syl)
	{
		if(!syl[i].length)
		{
			continue;
		}
		if(syl[i].indexOf("+") != -1)
		{
			var tmp1 = syl[i].slice(0, syl[i].indexOf("+"));
			var tmp2 = syl[i].slice(syl[i].indexOf("+"));
			if(!tmp1.length)
			{
				continue;
			}
			document.getElementById('display').innerHTML += "<img src=\"" + url + "kana/" + tmp1 + ".png\" />";
			//log("<img src=\"" + url + "kana/" + tmp1 + ".png\" />");
			if(!tmp2.length)
			{
				continue;
			}
			document.getElementById('display').innerHTML += "<img src=\"" + url + "kana/" + tmp2 + ".png\" />";
			//log("<img src=\"" + url + "kana/" + tmp2 + ".png\" width=\"35\"/>");
		}
		else
		{
			document.getElementById('display').innerHTML += "<img src=\"" + url + "kana/" + syl[i] + ".png\" />";
			//log("<img src=\"" + url + "kana/" + syl[i] + ".png\" />");
		}
	}
	//log("-----------------------------------------------------------");
}